package com.esurvey.core;
import javax.mail.*;
import javax.mail.internet.*;

import java.io.InputStream;
import java.util.Properties;

public class SMTP {
		public static void send(String to, String subject, String body) throws Exception {
		
		Properties props = new Properties();
		InputStream s = eSurveyUtilities.getResource("smtp.properties");
		props.load(s);
		
		Session session = Session.getInstance(props);
		
		if (eSurveyUtilities.str2boo(props.getProperty("mail.smtp.auth"))) {
		session.setPasswordAuthentication(new URLName(props.getProperty("mail.smtp.host")), new PasswordAuthentication("mail.smtp.user","mail.smtp.pass"));
		}
		
		MimeMessage mensaje = new MimeMessage(session);
		mensaje.setFrom(new InternetAddress(props.getProperty("mail.smtp.from")));
		mensaje.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
		mensaje.setSubject(subject);
		mensaje.setText(body);
		
		Transport mta = session.getTransport("smtp");
		mta.connect();
		Transport.send(mensaje);
		mta.close();  
	}
}
